package com.ocgs.web.action.customer;

import com.ocgs.base.core.constant.BaseConstants;
import com.ocgs.base.core.exception.BaseServiceException;
import com.ocgs.base.core.web.action.BaseAction;
import com.ocgs.base.util.DateUtil;
import com.ocgs.code.model.Code;
import com.ocgs.code.service.impl.CodeServiceImpl;
import com.ocgs.model.Customer;
import com.ocgs.service.impl.CustomerServiceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author bryant
 * @version 1.0
 * @since 2012-05-14
 *
 */
public class CustomerManageAction extends BaseAction{

    private static final long serialVersionUID = -2484651244734670052L;
    private CustomerServiceImpl customerService;
    private CodeServiceImpl codeService;
    private List<Customer> customerList;
    private List<Code> townList = new ArrayList<Code>();
    private List<Code> villageList = new ArrayList<Code>();
    private Customer customer;
    private String[] checkBox;
    public CustomerServiceImpl getCustomerService() {
        return customerService;
    }

    public void setCustomerService(CustomerServiceImpl customerService) {
        this.customerService = customerService;
    }

    public CodeServiceImpl getCodeService() {
        return codeService;
    }

    public void setCodeService(CodeServiceImpl codeService) {
        this.codeService = codeService;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public String[] getCheckBox() {
        return checkBox;
    }

    public void setCheckBox(String[] checkBox) {
        this.checkBox = checkBox;
    }

    public List<Customer> getCustomerList() {
        return customerList;
    }

    public void setCustomerList(List<Customer> customerList) {
        this.customerList = customerList;
    }

    public List<Code> getTownList() {
        return townList;
    }

    public void setTownList(List<Code> townList) {
        this.townList = townList;
    }

    public List<Code> getVillageList() {
        return villageList;
    }

    public void setVillageList(List<Code> villageList) {
        this.villageList = villageList;
    }

    public String loadCustomerInsertPage() throws BaseServiceException {
        return BaseConstants.ACTION_RESULT_INSERT;
    }

    public String linkCustomerUpdatePage() throws BaseServiceException{
        try{
            customer =  customerService.selectByPrimaryKey(customer.getId());
            this.townList = codeService.findChildCodeList(BaseConstants.CodeType.CODE_TYPE_MUNICIPIO,customer.getMunicipioId());
            this.villageList = codeService.findChildCodeList(BaseConstants.CodeType.CODE_TYPE_TOWN,customer.getTownId());
        }catch (BaseServiceException e){
            throw new BaseServiceException(e);
        }
        return BaseConstants.ACTION_RESULT_UPDATE;
    }

    public String loadCustomerSearchPage() throws BaseServiceException{

        return BaseConstants.ACTION_RESULT_SEARCH;
    }

    public String search() throws BaseServiceException {
        try {
            paginatedList = customerService.findAllCustomerForPagination(init(getRequest(), paginatedListImpl), customer);
        } catch (BaseServiceException e) {
            throw new BaseServiceException(e);
        }
        if(paginatedList==null)
            addActionMessage(this.getText(BaseConstants.MESSAGE_NO_SEARCH_RESULT));
        this.townList = codeService.findChildCodeList(BaseConstants.CodeType.CODE_TYPE_MUNICIPIO,customer.getMunicipioId());
        this.villageList = codeService.findChildCodeList(BaseConstants.CodeType.CODE_TYPE_TOWN,customer.getTownId());
        return BaseConstants.ACTION_RESULT_SEARCH;
    }

    public String insert() throws BaseServiceException {
        try {
            customer.setOnCreated(DateUtil.getCurrentTimestamp());
            customer.setStatus(BaseConstants.ACTIVE_STATUS);
            customer.setAddress(customerService.retrieveCustomerAddress(customer));
            customerService.insert(customer);
        } catch (BaseServiceException e) {
            e.printStackTrace();
            throw e;
        }
        customer = new Customer();
        addActionMessage(this.getText(BaseConstants.MESSAGE_ADD_SUCCESSFUL));
        return BaseConstants.ACTION_RESULT_INSERT;
    }

    public String update() throws BaseServiceException {
        try {
            customer.setLastUpdateTime(new Date());
            customerService.updateByPrimaryKeySelective(customer);
        } catch (BaseServiceException e) {
            e.printStackTrace();
            throw e;
        }
        addActionMessage(this.getText(BaseConstants.MESSAGE_UPDATE_SUCCESSFUL));
        return BaseConstants.ACTION_RESULT_UPDATE;
    }

    public String delete() throws BaseServiceException {
        try {
            if(checkBox!=null&&checkBox.length!=0){
                Customer customer = new Customer();
                for(String id:checkBox){
                    customer.setStatus(BaseConstants.DELETE_STATUS);
                    customer.setId(Integer.parseInt(id));
                    customerService.updateByPrimaryKeySelective(customer);
                }
            }else{
                customer.setStatus(BaseConstants.DELETE_STATUS);
                customerService.updateByPrimaryKeySelective(customer);
            }
            paginatedList = customerService.findAllCustomerForPagination(init(getRequest(), paginatedListImpl), customer);
        } catch (BaseServiceException e) {
            e.printStackTrace();
            throw e;
        }
        addActionMessage(this.getText(BaseConstants.MESSAGE_DELETE_SUCCESSFUL));
        return BaseConstants.ACTION_RESULT_SEARCH;
    }
}
